package org.corey.wyp.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	
	import org.corey.wyp.business.PropertyManagerDelegate;
	import org.corey.wyp.event.TodayAccountItemAddEvent;
	import org.corey.wyp.model.ProperityModelLocator;
	import org.corey.wyp.vo.AccountItem;
	import org.corey.wyp.vo.OperationType;
	
	public class TodayAccountItemAddCommand implements ICommand,IResponder
	{	
		private var item:AccountItem;
		private var operationType:String;
		
		public function TodayAccountItemAddCommand()
		{
		}
		
		public function execute(event:CairngormEvent):void{
			var addEvent:TodayAccountItemAddEvent=TodayAccountItemAddEvent(event);
			item=AccountItem(addEvent.data);
			if(item.id==0){
				operationType=OperationType.ADD;
			}else{
				operationType=OperationType.UPDATE;
			}
			var pm:PropertyManagerDelegate=new PropertyManagerDelegate(this);
			item.calendar=ProperityModelLocator.getInstance().application.today;
			pm.saveAccountItem(item);
		}
		
		public function result(event:Object):void{
			if(int(event.result)>0){
				if(operationType==OperationType.ADD){
					item.id=int(event.result);
					ProperityModelLocator.getInstance().application.addTodayAccountItem(item);
				}else if(operationType==OperationType.UPDATE){
					ProperityModelLocator.getInstance().application.today.accountItems.refresh();
				}
			}
		}
		
		public function fault(info:Object):void{
			Alert.show("增加信息失败:"+info);
		}
	}
}